<template> 
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['sys:role:create']"
                  >新增</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button
                    type="success"
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['sys:role:update']"
                  >修改</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['sys:role:delete']"
                  >删除</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button
                    type="warning"
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['system:post:export']"
                  >导出</el-button>
                </el-col>
              </el-row>
    <div class="table-container">
      <el-table ref="SmsBasicGiftsTable"
                :data="list"
                style="width: 100%;"
                @selection-change="handleSelectionChange"
                v-loading="listLoading" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="120" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="活动类型" align="center" width="120">
          <template slot-scope="scope">{{scope.row.bigType | formatActiviType}}</template>
        </el-table-column>
        <el-table-column label="优惠类型" width="120" align="center">
          <template slot-scope="scope">{{scope.row.smallType | formatCouponType(scope.row)}}</template>
        </el-table-column>
        <el-table-column label="活动名称" width="120" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="活动时间" width="220" align="center">
          <template slot-scope="scope">
            <p>开始时间：{{scope.row.startTime | formatTime}}</p>
            <p>到期时间：{{scope.row.endTime | formatTime}}</p>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="120" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleUpdateStatus(scope.$index, scope.row)"
              :active-value="0"
              :inactive-value="1"
              v-model="scope.row.status">
            </el-switch>
          </template>
        </el-table-column>
  <el-table-column label="活动规则" width="240" align="center">
          <template slot-scope="scope">{{scope.row.rules|filterRule(scope.row)}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="text"
                       @click="handleUpdate(scope.row)">编辑
            </el-button>
            <el-button size="mini"
                       type="text"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select
        size="small"
        v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small">
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
  import {fetchList,updateStatus,deleteSmsBasicGifts} from '@/api/sms/smsBasicGifts';
  import {formatDate} from '@/utils/date';
  const defaultListQuery = {
    pageNum: 1,
    pageSize: 5,
    name: null,
    type: null,
    endTime:null
  };
  const defaultTypeOptions = [
    {
      label: 'PC首页轮播',
      value: 0
    },
    {
      label: 'APP首页轮播',
      value: 1
    }
  ];
  export default {
    name: 'SmsBasicGiftsList',
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        typeOptions: Object.assign({}, defaultTypeOptions),
        list: null,
        // 非单个禁用
                   single: true,
                   // 非多个禁用
                   multiple: true,
                     // 总条数
                    total: 0,
                    isEdit:false,
                     // 弹出层标题
                          title: "",
                   // 是否显示弹出层
                     open: false,
        listLoading: false,
        multipleSelection: [],
        operates: [
          {
            label: "删除",
            value: 0
          }
        ],
        operateType: null
      }
    },
    created() {
      this.getList();
    },
    filters:{
      filterRule(value){
        console.log(value);

        let result='';
        let attr = JSON.parse(value);
        console.log(attr);
        if (attr){
          for(let i=0;i<attr.length;i++){
            if (attr[i].fullPrice>0){
              result='满';
              result+=attr[i].fullPrice;
              result+="元:减";
              result+=attr[i].reducePrice;
              result+=";";
            }

          }
        }

        return result;

      },
      formatType(type){
        if(type===1){
          return 'APP首页轮播';
        }else{
          return 'PC首页轮播';
        }
      },
       formatActiviType(useType){
              if(useType===1){
                return '首购礼';
              }else if(useType===2){
                return '满购礼';
              }else{
                return '单品礼购';
              }
            },
             formatCouponType(useType){

                     if(useType===1){
                                    return '第一单获取';
                                  }else if(useType===2){
                                    return '所有订单获取';
                                  }else{
                                    return '消费金额';
                                  }
                  },
      formatTime(time){
        if(time==null||time===''){
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      },
    },
    methods: {
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
      },
      handleSearchList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleSelectionChange(val){
        this.multipleSelection = val;
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleUpdateStatus(index,row){
      console.log(row);
        this.$confirm('是否要修改启用状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
         let params = new URLSearchParams();
                params.append('id', row.id);
                params.append('status', row.status);
                 params.append('bigType', row.bigType);

          updateStatus(params).then(response=>{
            this.getList();
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
          });
        }).catch(() => {
          this.$message({
            type: 'success',
            message: '已取消操作!'
          });
          this.getList();
        });
      },
      handleDelete(index,row){
        this.deleteSmsBasicGifts(row.id);
      },
      handleBatchOperate(){
        if (this.multipleSelection < 1) {
          this.$message({
            message: '请选择一条记录',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        let ids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id);
        }
        if(this.operateType===0){
          //删除
          this.deleteHomeAdvertise(ids);
        }else {
          this.$message({
            message: '请选择批量操作类型',
            type: 'warning',
            duration: 1000
          });
        }
      },
      handleAdd(){
        this.$router.push({path: '/sms/addSmsBasicGifts'})
      },
      handleUpdate(index,row){
        this.$router.push({path: '/sms/updateSmsBasicGifts', query: {id: row.id}})
      },
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.records;
          this.total = response.data.total;
        })
      },
      deleteHomeAdvertise(ids){

        this.$confirm('是否确认要删除该广告?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params=new URLSearchParams();
          params.append("ids",ids);
          deleteHomeAdvertise(params).then(response=>{
            this.getList();
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          });
        })
      },
      /** 新增按钮操作 */
               handleAdd() {
                 this.reset();

                 this.open = true;
                 this.title = "添加角色";
               },
               /** 修改按钮操作 */
               handleUpdate(row) {
                 this.reset();
                 this.isEdit=true;
                 const id = row.id || this.ids

                 getBrand(id).then(response => {
                   this.brand = response.data;
                   this.open = true;
                   this.title = "修改角色";
                 });
               },
                /** 导出按钮操作 */
                   handleExport() {
                     const queryParams = this.queryParams;
                     this.$confirm('是否确认导出所有角色数据项?', "警告", {
                         confirmButtonText: "确定",
                         cancelButtonText: "取消",
                         type: "warning"
                       }).then(function() {
                         return exportRole(queryParams);
                       }).then(response => {
                         this.download(response.msg);
                       }).catch(function() {});
                   },
                // 取消按钮
                   cancel() {
                     this.open = false;
                     this.reset();
                   },
                     // 多选框选中数据
                       handleSelectionChange(selection) {
                         this.ids = selection.map(item => item.id)
                         this.single = selection.length!=1
                         this.multiple = !selection.length
                       },
                   /** 重置按钮操作 */
                       resetQuery() {
                         this.dateRange = [];
                         this.resetForm("queryForm");
                         this.handleQuery();
                       },
                        // 表单重置
                           reset() {

                             this.brand = {
                               id: undefined,
                               name: undefined,

                               sort: 0,
                               status: "0",

                               description: undefined
                             };
                             this.resetForm("form");
                           },
    }
  }
</script>
<style scoped>
  .input-width {
    width: 203px;
  }
</style>


